const showToast = ref(false)
const showLoading = ref(false)

export const info = (title, duration = 1500) => {
  msg({
    mask: true,
    icon: 'none',
    title,
    duration,
  })
}

export const success = (title, duration = 1500) => {
  msg({
    mask: true,
    icon: 'success',
    title,
    duration,
  })
}

export const error = (title, duration = 1500) => {
  msg({
    mask: true,
    icon: 'error',
    title,
    duration,
  })
}

export const loading = (status) => {
  // 优先展示toast提示信息
  if (status && !showLoading.value) {
    // show
    uni.showLoading({
      title: '请稍后',
    })
    showLoading.value = true
  } else {
    // hide
    if (!showToast.value) {
      uni.hideLoading()
      showLoading.value = false
    }
  }
}

export const msg = (params) => {
  // 覆盖loading提示
  uni.showToast({
    mask: true,
    ...params,
  })
  showToast.value = true
  if (showLoading.value) {
    uni.hideLoading()
    showLoading.value = false
  }
  setTimeout(() => {
    showToast.value = false
  }, params.duration)
}

export default {
  info,
  success,
  error,
  loading
}